<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- dynamicview-tutorial.qdoc -->
  <title>QML Dynamic View Ordering Tutorial 2 - Dragging View Items | Qt Quick 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtquick-index.html">Qt Quick 模块</a></td><td >QML 动态视图排序教程 2 - 拖动视图项</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="qtquick-tutorials-dynamicview-dynamicview1-example.html" />
  <link rel="next" href="qtquick-tutorials-dynamicview-dynamicview3-example.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-dynamicview-dynamicview1-example.html">QML 动态视图排序教程 1 - 一个简单的 ListView 和委托</a>
<span class="naviSeparator">  &#9702;  
<a class="nextPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-dynamicview-dynamicview3-example.html">QML 动态视图排序教程 3 - 移动拖动的项目</a></span>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level2"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-dynamicview-dynamicview2-example.html#walkthrough">演练</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QML 动态视图排序教程 2 - 拖动视图项</h1>
<span class="subtitle"></span>
<!-- $$$tutorials/dynamicview/dynamicview2-description -->
<div class="descr"> <a name="details"></a>
<p>在我们有了一个可见的项列表，我们希望能够与它们进行交互。我们将从扩展委托开始，以便可以在屏幕上上下拖动可见内容。更新后的委托如下所示：</p>
<pre class="qml">

      <span class="type"><a href="../qtqml/qml-qtqml-component.html">Component</a></span> {
          <span class="name">id</span>: <span class="name">dragDelegate</span>

          <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> {
              <span class="name">id</span>: <span class="name">dragArea</span>

              property <span class="type"><a href="../qtqml/qml-bool.html">bool</a></span> <span class="name">held</span>: <span class="number">false</span>

              <span class="type">anchors</span> { <span class="name">left</span>: <span class="name">parent</span>.<span class="name">left</span>; <span class="name">right</span>: <span class="name">parent</span>.<span class="name">right</span> }
              <span class="name">height</span>: <span class="name">content</span>.<span class="name">height</span>

              <span class="name">drag</span>.target: <span class="name">held</span> ? <span class="name">content</span> : <span class="name">undefined</span>
              <span class="name">drag</span>.axis: <span class="name">Drag</span>.<span class="name">YAxis</span>

              <span class="name">onPressAndHold</span>: <span class="name">held</span> <span class="operator">=</span> <span class="number">true</span>
              <span class="name">onReleased</span>: <span class="name">held</span> <span class="operator">=</span> <span class="number">false</span>

              <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
                  <span class="name">id</span>: <span class="name">content</span>
                  <span class="type">anchors</span> {
                      <span class="name">horizontalCenter</span>: <span class="name">parent</span>.<span class="name">horizontalCenter</span>
                      <span class="name">verticalCenter</span>: <span class="name">parent</span>.<span class="name">verticalCenter</span>
                  }
                  <span class="name">width</span>: <span class="name">dragArea</span>.<span class="name">width</span>; <span class="name">height</span>: <span class="name">column</span>.<span class="name">implicitHeight</span> <span class="operator">+</span> <span class="number">4</span>

                  <span class="name">border</span>.width: <span class="number">1</span>
                  <span class="name">border</span>.color: <span class="string">&quot;lightsteelblue&quot;</span>
                  <span class="name">color</span>: <span class="name">dragArea</span>.<span class="name">held</span> ? <span class="string">&quot;lightsteelblue&quot;</span> : <span class="string">&quot;white&quot;</span>
                  Behavior on <span class="name">color</span> { <span class="type"><a href="qml-qtquick-coloranimation.html">ColorAnimation</a></span> { <span class="name">duration</span>: <span class="number">100</span> } }
                  <span class="name">radius</span>: <span class="number">2</span>
                  <span class="name">states</span>: <span class="name">State</span> {
                      <span class="name">when</span>: <span class="name">dragArea</span>.<span class="name">held</span>

                      <span class="type"><a href="qml-qtquick-parentchange.html">ParentChange</a></span> { <span class="name">target</span>: <span class="name">content</span>; <span class="name">parent</span>: <span class="name">root</span> }
                      <span class="type"><a href="qml-qtquick-anchorchanges.html">AnchorChanges</a></span> {
                          <span class="name">target</span>: <span class="name">content</span>
                          <span class="type">anchors</span> { <span class="name">horizontalCenter</span>: <span class="name">undefined</span>; <span class="name">verticalCenter</span>: <span class="name">undefined</span> }
                      }
                  }
                  <span class="type"><a href="qml-qtquick-column.html">Column</a></span> {
                      <span class="name">id</span>: <span class="name">column</span>
                      <span class="type">anchors</span> { <span class="name">fill</span>: <span class="name">parent</span>; <span class="name">margins</span>: <span class="number">2</span> }

                      <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Name: '</span> <span class="operator">+</span> <span class="name">name</span> }
                      <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Type: '</span> <span class="operator">+</span> <span class="name">type</span> }
                      <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Age: '</span> <span class="operator">+</span> <span class="name">age</span> }
                      <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Size: '</span> <span class="operator">+</span> <span class="name">size</span> }
                  }
              }
          }
      }

</pre>
<a name="walkthrough"></a>
<h3 >演练</h3>
<p>这里的主要变化是委托的根项现在是一个<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-mousearea.html">MouseArea</a>，它为鼠标事件提供处理程序，并允许我们拖动委托的内容项。它还充当内容项的容器，这很重要，因为代理的根项由视图定位并且不能通过其他方式移动。</p>
<pre class="qml">

          <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> {
              <span class="name">id</span>: <span class="name">dragArea</span>

              property <span class="type"><a href="../qtqml/qml-bool.html">bool</a></span> <span class="name">held</span>: <span class="number">false</span>

              <span class="type">anchors</span> { <span class="name">left</span>: <span class="name">parent</span>.<span class="name">left</span>; <span class="name">right</span>: <span class="name">parent</span>.<span class="name">right</span> }
              <span class="name">height</span>: <span class="name">content</span>.<span class="name">height</span>

              <span class="name">drag</span>.target: <span class="name">held</span> ? <span class="name">content</span> : <span class="name">undefined</span>
              <span class="name">drag</span>.axis: <span class="name">Drag</span>.<span class="name">YAxis</span>

              <span class="name">onPressAndHold</span>: <span class="name">held</span> <span class="operator">=</span> <span class="number">true</span>
              <span class="name">onReleased</span>: <span class="name">held</span> <span class="operator">=</span> <span class="number">false</span>

              <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
                  <span class="name">id</span>: <span class="name">content</span>
              }
          }

</pre>
<p>通过将内容项绑定到<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-mousearea.html">MouseArea</a>的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-mousearea.html#drag.target-prop">drag.target</a>属性来启用拖动内容项。因为我们仍然希望视图是可轻弹的，所以我们等到<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-mousearea.html">MouseArea</a>的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-mousearea.html#pressAndHold-signal">pressAndHold</a>信号发出后，再绑定拖动目标。这样，当鼠标在保持超时到期之前移动时，它被解释为移动列表，如果它在被解释为拖动项目之后移动。为了让用户更清楚何时可以拖动项目，我们将在超时到期时更改内容项目的背景颜色。</p>
<pre class="qml">

                  <span class="name">color</span>: <span class="name">dragArea</span>.<span class="name">held</span> ? <span class="string">&quot;lightsteelblue&quot;</span> : <span class="string">&quot;white&quot;</span>
                  Behavior on <span class="name">color</span> { <span class="type"><a href="qml-qtquick-coloranimation.html">ColorAnimation</a></span> { <span class="name">duration</span>: <span class="number">100</span> } }

</pre>
<p>在可以拖动项目之前，我们需要做的另一件事是取消设置内容项目上的任何锚点，以便可以自由移动它。我们在持有委托项时触发的状态更改中执行此操作，同时我们可以将内容项重新设置为根项，以便在堆叠顺序中位于其他项之上，并且在拖动时不会被遮挡大约。</p>
<pre class="qml">

                  <span class="name">states</span>: <span class="name">State</span> {
                      <span class="name">when</span>: <span class="name">dragArea</span>.<span class="name">held</span>

                      <span class="type"><a href="qml-qtquick-parentchange.html">ParentChange</a></span> { <span class="name">target</span>: <span class="name">content</span>; <span class="name">parent</span>: <span class="name">root</span> }
                      <span class="type"><a href="qml-qtquick-anchorchanges.html">AnchorChanges</a></span> {
                          <span class="name">target</span>: <span class="name">content</span>
                          <span class="type">anchors</span> { <span class="name">horizontalCenter</span>: <span class="name">undefined</span>; <span class="name">verticalCenter</span>: <span class="name">undefined</span> }
                      }
                  }

</pre>
<p>文件：</p>
<ul>
<li><a href="qtquick-tutorials-dynamicview-dynamicview2-petsmodel-qml.html">tutorials/dynamicview/dynamicview2/PetsModel.qml</a></li>
<li><a href="qtquick-tutorials-dynamicview-dynamicview2-dynamicview-qml.html">tutorials/dynamicview/dynamicview2/dynamicview.qml</a></li>
<li><a href="qtquick-tutorials-dynamicview-dynamicview2-dynamicview2-qmlproject.html">tutorials/dynamicview/dynamicview2/dynamicview2.qmlproject</a></li>
</ul>
</div>
<!-- @@@tutorials/dynamicview/dynamicview2 -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-dynamicview-dynamicview1-example.html">QML 动态视图排序教程 1 - 一个简单的 ListView 和委托</a>
<span class="naviSeparator">  &#9702;  
<a class="nextPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-dynamicview-dynamicview3-example.html">QML 动态视图排序教程 3 - 移动拖动的项目</a></span>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
